var topbarTemplate = '' +
    '<div class="navbar-header">' +
    '<a class="navbar-brand" href="#">冲吧APP后台运营系统</a>' +
    '</div>' +
    '<ul  style="visibility:hidden" class="nav navbar-nav navbar-right">' +

    '<li class="dropdown">' +
    '<a href="#" class="dropdown-toggle" data-toggle="dropdown">管理员<span class="caret"></span></a>' +
    '<ul class="dropdown-menu" role="menu">' +
    /*'<li><a href="javascript:;">设置</a></li>'+*/
    '<li><a href="javascript:;" data-opuser-id="" id="resetPwd">修改密码</a></li>' +
    '<li class="divider"></li>' +
    '<li><a href="javascript:;" id="outLogin">退出</a></li>' +
    '</ul>' +
    '</li>' +
    '</ul>' +
 /*<!-- 弹出编辑框 -->*/
    '<input type="hidden" id="company_hidden" value="" >'+
    '<div class="row modal fade" id="myModal_top_rePwd" style="top:0px;left:0px;right:0px;bottom:0px;overflow: inherit;overflow-x: hidden;overflow-y: auto;">'+
        '<div class="modal-dialog">'+
            '<div class="modal-content">'+
                '<div class="modal-header">'+
                    '<button type="button" class="close" data-dismiss="modal">'+
                        '<span aria-hidden="true">&times;</span>'+
                        '<span class="sr-only">Close</span>'+
                    '</button>'+
                '</div>'+
                '<div class="modal-body">'+
    '<div class=" modal2_top" style="top:50px" id="addIndex2_top"> ' +
    '<div class="row">' +
    '<div class="col-xs-4 lineHeight width110px" style="margin-left:20px;">旧密码 :</div>' +
    '<div class="col-xs-8">' +
    '<input type="password" id="oldpassword" require_top="password"  class="form-control voteId  " value="">' +
    '</div>' +

    '</div>' +
    '<div class="row">' +
    '<div class="col-xs-4 lineHeight width110px" style="margin-left:20px;">新密码 :</div>' +
    '<div class="col-xs-8">' +
    '<input type="password" id="newpassword" require_top="password" class="form-control voteId  " value="">' +
    '</div>' +

    '</div>' +
    '<div class="row">' +
    '   <div class="col-xs-4 lineHeight width110px" style="margin-left:20px;">确认新密码 :</div>' +
    '   <div class="col-xs-8">' +
    ' <input type="password" id="renewpassword" require_top="repassword"  class="form-control voteId  " value="">' +
    '</div>' +

    '</div>' +

    '<div class="row mt30 text-center">' +
    '<div class="col-xs-1 col-md-offset-1">' +
    '</div>' +
    '<div class="col-xs-3 col-md-offset-1">' +
    '<button type="button" style="" class="btn btn-primary savePwd" disabled="disabled" >保存</button>' +
    '</div>' +
    '<div class="col-xs-3">' +
    '<button type="button" class="btn btn-primary colse_rePwd"> 取消</button>' +
    '</div>' +
    '</div>' +
    '</div></div></div></div></div>'+
   
     /*弹出提示框*/
'<div class="row modal fade" id="myModal_topbar" style="top:0px;left:0px;right:0px;bottom:0px;overflow: inherit;">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
    '<div class="modal-header">' +
        '<button type="button" class="close" data-dismiss="modal">' +
            '<span aria-hidden="true">&times;</span>' +
            '<span class="sr-only">Close</span>' +
        '</button>' +
        '<h4 class="modal-title">提示</h4>' +
    '</div>' +
    '<div class="modal-body">' +
        '<p></p>' +
    '</div>' +
    '<div class="modal-footer">' +
        '<button type="button" class="btn btn-default"  data-dismiss="modal">取消</button>' +
        '<button type="button" class="btn btn-primary confirm_topbar" >确定</button>' +
   ' </div>' +
'</div>' +
'</div>' +
'</div>'+
/*<!-- 弹出提示框 -->*/
'<div class="row modal fade" id="myModal_topbar_success" style="top:0px;left:0px;right:0px;bottom:0px;overflow: inherit;">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
    '<div class="modal-header">' +
        '<button type="button" class="close" data-dismiss="modal">' +
            '<span aria-hidden="true">&times;</span>' +
            '<span class="sr-only">Close</span>' +
        '</button>' +
        '<h4 class="modal-title">提示</h4>' +
    '</div>' +
    '<div class="modal-body">' +
        '<p>恭喜您,密码修改成功</p>' +
    '</div>' +
    /*'<div class="modal-footer">' +
        '<button type="button" class="btn btn-default"  data-dismiss="modal">取消</button>' +
        '<button type="button" class="btn btn-primary confirm_topbar" >确定</button>' +
   ' </div>' +*/
'</div>' +
'</div>' +
'</div>'
$(function() {
    $('#outLogin').live("click",function () {
        $("#myModal_topbar .modal-body p").html("您确定要退出吗？");
        $('#myModal_topbar').modal('show');
    });
    $(".confirm_topbar").live("click",function(){
        $.getJSON(interfaces.logout, {}, function(json, textStatus) {
                if(json){
                setTimeout(function () {
                    window.location.href = "./index.html";
                }, 200);
            }else{
                alert("退出失败，请刷新页面");
            }
        });
    });
    // //top用户信息
    // getInterfaceInfo({
    //     url: interfaces.fetchUser,
    // }, function(data) {
    //     console.log(data)
    //     if (data == false) {
    //        // window.location.href = "./index.html";
    //     } else {
    //         interfaces.companyid=data.pmcid;
            var render = template.compile(topbarTemplate);
            var html = render({});
            $("#topbar").html(html);
    //     }
    // });
    
    $("#resetPwd").live("click", function() {
        opuserId_top = $(this).attr('data-opuser-id');
        $("#addIndex2_top input").val("");
        $(".error,.error_top").removeClass("error").removeClass("error_top");
        $('#addIndex2_top .tishi').empty();

        $("[require_top]").addClass("error_top");
        $('#myModal_top_rePwd').modal('show');
        setBtn();
        //floterShow('#addIndex2');
    });

    $("[require_top]").live("blur", function() {
        var type = $(this).attr("require_top");
        if (type == "password") {
            var e = password($(this).val().trim());
            $(this).next("span").empty();

            if (e) {
                /*$(this).removeClass("error");   
                $(this).removeClass("error_top");*/
                //判断旧密码是否正确
                if ($(this).attr("id").trim() == "oldpassword") {
                    getInterfaceInfo({
                        url: interfaces.showOpuser + '/' + opuserId_top,
                        data: {}
                    }, function(data) {
                        $.each(data.record, function(index, el) {
                            if (index == "password") {
                                if (hex_md5($("#oldpassword").val().trim()) != el) {
                                    $("#oldpassword").addClass("error");
                                    $("#oldpassword").addClass("error_top");
                                    $("#oldpassword").after("<span class='tishi' >原始密码错误</span>");
                                } else {
                                    $("#oldpassword").removeClass("error");
                                    $("#oldpassword").removeClass("error_top");
                                }
                                setBtn();
                            }
                        });
                        setBtn()
                    });
                } else {
                    $(this).removeClass("error");
                    $(this).removeClass("error_top");
                    if($("#renewpassword").val().trim() != ""){
                        $("#renewpassword").trigger('blur');
                    }
                }

            } else {
                $(this).addClass("error");
                $(this).addClass("error_top");
                $(this).next("span").empty();
                $(this).after("<span class='tishi' >密码格式错误</span>");
                setBtn();
            }
        }
        if (type == "repassword") {
            var pwd = $("#newpassword").val();
            var repassword = $(this).val();
            var e = rePassword(pwd.trim(), $(this).val().trim());
            $(this).next("span").empty();

            if (e) {
                $(this).removeClass("error");
                $(this).removeClass("error_top");
            } else {
                $(this).addClass("error");
                $(this).addClass("error_top");
                $(this).after("<span class='tishi' >两次密码不一致</span>");
            }
            setBtn();

        }
        

    });
    $(".savePwd").live("click", function() {
        var data = {
            password: hex_md5($("#newpassword").val().trim())
        };
        getInterfaceInfo({
            type: "PUT",
            url: interfaces.editOpuser + "/" + opuserId_top,
            data: data
        }, function(data) {
            $("#addIndex2").hide();
            if (data) {
                //alert("恭喜您,密码修改成功！");
                $("#myModal_top_rePwd").modal("hide");
                $("#myModal_topbar_success").modal("show");
            }
            setTimeout(function(){
                $("#myModal_topbar_success").modal("hide");
                //location.reload();
            },3000);
           
        });
    });
    $(".colse_rePwd").live("click",function(){
        $('#myModal_top_rePwd').modal('hide');
    });
});

//特殊情况下设置保存按钮是否可用
function saveBtn2() {
    var error_edit = $("#addIndex2 .error_2");
    if (error_edit == null || error_edit.length == 0) {
        $('.save').removeAttr("disabled");
    } else {
        $('.save').prop("disabled", true);
    }
    if (error_edit == null || error_edit.length == 0) {
        $('.saveEdit').removeAttr("disabled");
    } else {
        $('.saveEdit').prop("disabled", true);
    }
}

function setBtn(){
    //设置保存按钮是够可用
    var error_ = $(".error_top");
    if (error_ == null || error_.length == 0) {
        $('.savePwd').removeAttr("disabled");
    } else {
        $('.savePwd').prop("disabled", true);
    }
}